<?php
/**
 * Created by Command.
 * User: Vito
 */

namespace app\admin\curd\user;

use app\common\curd\Index;
use app\common\Enum;
use app\validate\UserValidate;
use app\model\RoomLeaseRecord;
use app\model\Room;
use app\model\Building;
class UserIndex extends Index
{
    /**
     * 关键词查询指定字段
     * @var string[]
     */
    protected $keywordQueryArgs = ['index'];
    //追加数据
    protected $appendCallback = [];
    //查询条件
    protected $where = [];
    protected $with = [];
    //字段
    protected $field = [];
    //排除字段
    protected $withoutField = false;

    protected $queryField = ['sex', 'status', 'type'];

    protected $validate = [
        UserValidate::class => Enum::VALID_LIST_SCENE,
    ];

    protected $labelCallback = [
        'get_status_enum'   => ['name' => '状态'],
        'get_platform_enum' => ['name' => '类型'],
        'get_sex_enum'      => ['name' => '性别'],
        'get_type_enum'     => ['name' => '用户类型'],
    ];
    protected function formatMiddleware($next)
    {
        $response = $next();
        // $housing = new Housing;
        // $arr = $housing->getDetail();
        // print_r(count($response['buildingg']));

        // die;
        foreach ($response as $key => $value) {
            $where['user_id'] = $value['id'];
            $where['status'] = 1;
            $response[$key]['room_name'] = '';
            $response[$key]['housing_name'] = '';
            $response[$key]['building_name'] = '';
            $RoomLeaseRecord = RoomLeaseRecord::where($where)->find();
            
            if ($RoomLeaseRecord) {
                $roomWhere['id'] = $RoomLeaseRecord['room_id'];
                $room = Room::where($roomWhere)->find();
                if($room){
                    $response[$key]['room_name'] = $room['name'];
                    $buildWhere['id']  = $room['building_number'];
                    $build = Building::where($buildWhere)->find();
                    $response[$key]['housing_name'] = $build['name'];
                    $response[$key]['building_name'] = $build['building_number'];
                }
                // halt($response[$key]['room_name']);
            }
        }
        return $response;
    }
}
